class CryptoTool
{

    protected $key = 'aaaabbbbccccc';
    protected $iv = 'dddddeeeffff';
    protected $cipher = "aes-256-cbc";

    function __construct()
    {
        $date = date('Ymd');
        //  key生成规则：key:+原始key+年月日得到字符串1进行md5加密，加密后从字符串第8位开始截取16个字符，示例：substr(md5('key:BcZ5k!LqqDVx20230101'),8,16)
        $this->key = md5('key:' . $this->key . $date);
        //  iv生成规则：iv:+原始iv+年月日得到字符串1进行md5加密，加密后从字符串第8位开始截取16个字符，示例：substr(md5('iv:Bgt$#wWrwFzy20230101'),8,16)
        $this->iv = substr(md5('iv:' . $this->iv . $date), 8, 16);
    }

    /**
     * 加密
     */
    public function encrypt($data)
    {
        $result = base64_encode(openssl_encrypt($data, $this->cipher, $this->key, true, $this->iv));
        return $result;
    }

    /**
     * 解密
     */
    public function decrypt($data)
    {
        $result = openssl_decrypt(base64_decode($data), $this->cipher, $this->key, true, $this->iv);
        return $result;
    }
}